package be.phl.model;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import be.phl.obstacleapp.MainData;
import be.phl.webservice.Building;
import be.phl.webservice.BuildingDetail;
import be.phl.webservice.BuildingSpec;
import be.phl.webservice.BuildingType;
import be.phl.webservice.City;
import be.phl.webservice.ItemProblem;
import be.phl.webservice.MaritalStatus;
import be.phl.webservice.MovementRestriction;
import be.phl.webservice.Patient;
import be.phl.webservice.PatientBundle;
import be.phl.webservice.PatientDetail;
import be.phl.webservice.PatientMovementRestriction;
import be.phl.webservice.PatientSpec;
import be.phl.webservice.PatientUsedAid;
import be.phl.webservice.RoomItem;
import be.phl.webservice.RoomType;
import be.phl.webservice.ScreeningBundle;
import be.phl.webservice.UsedAid;
import be.phl.webservice.UserFunction;

/**
 * 
 * DBAdapter inserts all the data from the webservice in the database
 * 
 * @author krisdelanghe
 * 
 */
public class DBAdapterDownloadBundle extends DBAdapter {

	public DBAdapterDownloadBundle(Context ctx) {
		super(ctx);
		// TODO Auto-generated constructor stub
	}

	/**
	 * insert all the data needed to make screenings
	 * 
	 * @param roomTypes
	 * @param roomItems
	 * @param itemProblems
	 */
	public void insertRoomData(List<RoomType> roomTypes, List<RoomItem> roomItems, List<ItemProblem> itemProblems) {
		ContentValues args;

		for (RoomType roomType : roomTypes) {

			args = new ContentValues();

			args.put(KEY_ROWID, roomType.getId());
			args.put("Name", roomType.getName());
			args.put("Activated", roomType.getActivated());

			if (db.update("RoomType", args, KEY_ROWID + "=" + roomType.getId(),
					null) == 0) {
				db.insert("RoomType", null, args);
			}
		}

		for (RoomItem roomItem : roomItems) {

			args = new ContentValues();

			args.put(KEY_ROWID, roomItem.getId());
			args.put("RoomTypeId", roomItem.getRoomType());
			args.put("Name", roomItem.getName());
			args.put("Activated", roomItem.getActivated());

			if (db.update("RoomItem", args, KEY_ROWID + "=" + roomItem.getId(),
					null) == 0) {
				db.insert("RoomItem", null, args);
			}
		}

		for (ItemProblem itemProblem : itemProblems) {

			args = new ContentValues();

			args.put(KEY_ROWID, itemProblem.getId());
			args.put("RoomItemId", itemProblem.getRoomItem());
			args.put("ParentId", itemProblem.getParentId());
			args.put("Name", itemProblem.getName());
			args.put("HasValue", itemProblem.getHasValue());
			args.put("Activated", itemProblem.getActivated());
			args.put("SingleSelection", itemProblem.getSingleSelection());

			if (db.update("ItemProblem", args,
					KEY_ROWID + "=" + itemProblem.getId(), null) == 0) {
				db.insert("ItemProblem", null, args);
			}
		}
	}
	
	/**
	 * insert all the user functions
	 * 
	 * @param userFunctions
	 */
	public void insertUserData(List<UserFunction> userFunctions) {
		for (UserFunction userFunction : userFunctions) {

			ContentValues args = new ContentValues();

			args.put(KEY_ROWID, userFunction.getId());
			args.put("Name", userFunction.getName());

			if (db.update("UserFunction", args,
					KEY_ROWID + "=" + userFunction.getId(), null) == 0) {
				db.insert("UserFunction", null, args);
			}
		}
	}
	
	/**
	 * insert all the cities
	 * 
	 * @param cities
	 */
	public void insertCityData(List<City> cities) {
		for (City city : cities) {

			ContentValues args = new ContentValues();

			args.put(KEY_ROWID, city.getId());
			args.put("City", city.getCity());
			args.put("Zip", city.getZip());

			if (db.update("City", args, KEY_ROWID + "=" + city.getId(), null) == 0) {
				db.insert("City", null, args);
			}
		}
	}
	
	/**
	 * insert all the data off the patients
	 * 
	 * @param buildingSpecs
	 * @param buildingTypes
	 * @param maritalStatuses
	 * @param movementRestrictions
	 * @param usedAids
	 * @param patientSpecs
	 * @param patientBundles
	 */
	public void insertPatientData(List<BuildingSpec> buildingSpecs, List<BuildingType> buildingTypes,
			List<MaritalStatus> maritalStatuses, List<MovementRestriction> movementRestrictions,
			List<UsedAid> usedAids, List<PatientSpec> patientSpecs, List<PatientBundle> patientBundles) {
		ContentValues args;
		
		for (BuildingSpec buildingSpec : buildingSpecs) {

			args = new ContentValues();

			args.put(KEY_ROWID, buildingSpec.getId());
			args.put("BuildingKey", buildingSpec.getBuildingKey());
			args.put("Activated", buildingSpec.getActivated());
			args.put("Comment", buildingSpec.getComment());
			args.put("Required", buildingSpec.getRequired());

			if (db.update("BuildingSpec", args,
					KEY_ROWID + "=" + buildingSpec.getId(), null) == 0) {
				db.insert("BuildingSpec", null, args);
			}
		}

		for (BuildingType buildingType : buildingTypes) {

			args = new ContentValues();

			args.put(KEY_ROWID, buildingType.getId());
			args.put("Name", buildingType.getName());

			if (db.update("BuildingType", args,
					KEY_ROWID + "=" + buildingType.getId(), null) == 0) {
				db.insert("BuildingType", null, args);
			}
		}

		for (MaritalStatus maritalStatus : maritalStatuses) {

			args = new ContentValues();

			args.put(KEY_ROWID, maritalStatus.getId());
			args.put("Name", maritalStatus.getName());

			if (db.update("MaritalStatus", args, KEY_ROWID + "="
					+ maritalStatus.getId(), null) == 0) {
				db.insert("MaritalStatus", null, args);
			}
		}

		for (MovementRestriction movementRestriction : movementRestrictions) {

			args = new ContentValues();

			args.put(KEY_ROWID, movementRestriction.getId());
			args.put("MRKey", movementRestriction.getMrkey());
			args.put("Activated", movementRestriction.getActivated());

			if (db.update("MovementRestriction", args, KEY_ROWID + "="
					+ movementRestriction.getId(), null) == 0) {
				db.insert("MovementRestriction", null, args);
			}
		}

		for (UsedAid usedAid : usedAids) {

			args = new ContentValues();

			args.put(KEY_ROWID, usedAid.getId());
			args.put("Name", usedAid.getName());
			args.put("Activated", usedAid.getActivated());
			args.put("Type", usedAid.getType());

			if (db.update("UsedAid", args, KEY_ROWID + "=" + usedAid.getId(),
					null) == 0) {
				db.insert("UsedAid", null, args);
			}
		}

		for (PatientSpec patientSpec : patientSpecs) {

			args = new ContentValues();

			args.put(KEY_ROWID, patientSpec.getId());
			args.put("PatientKey", patientSpec.getPatientKey());
			args.put("Activated", patientSpec.getActivated());
			args.put("Comment", patientSpec.getComment());
			args.put("Required", patientSpec.getRequired());

			if (db.update("PatientSpec", args,
					KEY_ROWID + "=" + patientSpec.getId(), null) == 0) {
				db.insert("PatientSpec", null, args);
			}
		}

		for (PatientBundle patientBundle : patientBundles) {

			// patient data
			Patient patient = patientBundle.getPatient();
			args = new ContentValues();

			args.put(KEY_ROWID, patient.getId());
			args.put("FirstName", patient.getFirstName());
			args.put("LastName", patient.getLastName());
			args.put("Gender", patient.getGender());
			args.put("DayOfBirth", patient.getDayOfBirthString());
			args.put("Weight", patient.getWeight());
			args.put("Length", patient.getLength());
			args.put("BuildingId", patient.getBuilding().getId());
			args.put("MaritalStatus", patient.getMaritalStatus());
			args.put("TelNumber", patient.getTelNumber());
			args.put("Email", patient.getEmail());
			args.put("ContactFirstName", patient.getContactFirstName());
			args.put("ContactLastName", patient.getContactLastName());
			args.put("ContactRelation", patient.getContactRelation());
			args.put("contactTelNumber", patient.getContactTelNumber());
			args.put("ContactEmail", patient.getContactEmail());

			if (db.update("Patient", args, KEY_ROWID + "=" + patient.getId(),
					null) == 0) {
				db.insert("Patient", null, args);
			}

			for (PatientDetail patientDetail : patientBundle.getPatientDetail()) {

				args = new ContentValues();

				args.put("CentralId", patientDetail.getId());
				args.put("PatientId", patientDetail.getPatient());
				args.put("PatientSpecId", patientDetail.getPatientSpec());
				args.put("Value", patientDetail.getValue());

				if (db.update("PatientDetail", args, "CentralId" + "="
						+ patientDetail.getId(), null) == 0) {
					db.insert("PatientDetail", null, args);
				}
			}

			for (PatientUsedAid patientUsedAid : patientBundle
					.getPatientUsedAid()) {

				args = new ContentValues();

				args.put("CentralId", patientUsedAid.getId());
				args.put("PatientId", patientUsedAid.getPatient());
				args.put("UsedAidId", patientUsedAid.getUsedAid());

				if (db.update("PatientUsedAid", args, "CentralId" + "="
						+ patientUsedAid.getId(), null) == 0) {
					db.insert("PatientUsedAid", null, args);
				}
			}

			for (PatientMovementRestriction patientMovementRestriction : patientBundle
					.getPatientMovementRestriction()) {

				args = new ContentValues();

				args.put("CentralId", patientMovementRestriction.getId());
				args.put("PatientId", patientMovementRestriction.getPatient());
				args.put("MovementRestrictionId",
						patientMovementRestriction.getMovementRestriction());
				args.put("PatientUsedAidId",
						patientMovementRestriction.getPatientUsedAid());
				args.put("Value", patientMovementRestriction.getValue());
				if (db.update("PatientMovementRestriction", args, "CentralId"
						+ "=" + patientMovementRestriction.getId(), null) == 0) {
					db.insert("PatientMovementRestriction", null, args);
				}

			}

			args = new ContentValues();

			args.put(KEY_ROWID, patientBundle.getPatientUserId());
			args.put("PatientId", patient.getId());
			args.put("UserId", MainData.getLoggedUser().getId());

			if (db.update("PatientUser", args,
					KEY_ROWID + "=" + patientBundle.getPatientUserId(), null) == 0) {
				db.insert("PatientUser", null, args);
			}

			// building data
			Building building = patientBundle.getPatient().getBuilding();
			args = new ContentValues();

			args.put(KEY_ROWID, building.getId());
			args.put("Street", building.getStreet());
			args.put("Number", building.getNumber());
			args.put("CityId", building.getCity());
			args.put("YearOfConstruction",
					building.getYearOfConstructionString());
			args.put("BuildingTypeId", building.getBuildingType());

			if (db.update("Building", args, KEY_ROWID + "=" + building.getId(),
					null) == 0) {
				db.insert("Building", null, args);
			}

			for (BuildingDetail buildingDetail : patientBundle
					.getBuildingDetail()) {

				args = new ContentValues();

				args.put("CentralId", buildingDetail.getId());
				args.put("BuildingId", buildingDetail.getBuilding());
				args.put("BuildingSpecId", buildingDetail.getBuildingSpec());
				args.put("Value", buildingDetail.getValue());

				if (db.update("BuildingDetail", args, "CentralId" + "="
						+ building.getId(), null) == 0) {
					db.insert("BuildingDetail", null, args);
				}
			}
		}
	}
}
